import StarCard from '../PrefabJs/StarCard';
import { Constellation } from '../../Server/StorageCenter';
import ServerCenter from '../../Server/ServerCenter';
import GameConfig from '../../Server/Data/GameConfig';
import SoundUtil from '../../Fantasy/Util/SoundUtil';


const { ccclass, property } = cc._decorator;

@ccclass
export default class GiftBox extends cc.Component {

    @property([cc.Node])
    menu: cc.Node[] = [];

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    starCard: cc.Prefab = null;
    // LIFE-CYCLE CALLBACKS:

    CardPool: cc.NodePool = new cc.NodePool();
    CardLock: Constellation = null;
    currMenu = '';
    onLoad() {
        for (let i = 0; i < 12; i++) {
            let node = cc.instantiate(this.starCard);
            this.CardPool.put(node);
        }
        this.CardLock = ServerCenter.getUserStar();
    }

    onEnable() {
        this.menuBack(null,'全部');
    }

    getStarCard() {
        if (this.CardPool.size() > 0) {
            return this.CardPool.get();
        }
        else {
            let node = cc.instantiate(this.starCard);
            return node;
        }
    }
    btnCallback(target,data){
        SoundUtil.playEffect("music/click");
        if (data == 'close'){
            this.node.active = false;
        }
        else if (data == 'explain'){

        }
    }
    menuBack(target, data) {
        if (data == this.currMenu) {
            return;
        }
        else {
            this.currMenu = data;
        }
        if (data == '全部') {
            this.menuShow(0);
            this.showUnLock();
        }
        else if (data == '石器') {
            this.menuShow(1);
            this.refreshContent(1);
        }
        else if (data == '青铜') {
            this.menuShow(2);
            this.refreshContent(2);
        }
        else if (data == '白银') {
            this.menuShow(3);
            this.refreshContent(3);
        }
        else if (data == '黄金') {
            this.menuShow(4);
            this.refreshContent(4);
        }
    }

    menuShow(index) {
        for (let i = 0; i < this.menu.length; i++) {
            if (index == i) {
                cc.find('white', this.menu[i]).active = false;
                cc.find('yellow', this.menu[i]).active = true;
            }
            else {
                cc.find('white', this.menu[i]).active = true;
                cc.find('yellow', this.menu[i]).active = false;
            }
        }
    }
    refreshContent(level) {
        for (let node of [].concat(this.content.children)) {
            node.getChildByName('Sprite').spriteFrame = null;
            this.CardPool.put(node);
        }
        Log('节点数量:', this.content.childrenCount);
        for (let i = 0; i < 12; i++) {
            let node = this.getStarCard();
            let script: StarCard = node.getComponent('StarCard');
            let name = GameConfig.getStarEnglish(i);
            let lock = this.CardLock[name] < level;
            script.setStarCard(i, level, lock);
            node.parent = this.content;
        }
    }
    showUnLock() {
        for (let node of [].concat(this.content.children)) {
            this.CardPool.put(node);
        }
        for (let i = 0; i < 12; i++) {

            let name = GameConfig.getStarEnglish(i);
            let level = this.CardLock[name];
            if (level > 0) {
                let node = this.getStarCard();
                let script: StarCard = node.getComponent('StarCard');
                script.setStarCard(i, level, false);
                node.parent = this.content;
            }
        }
    }
}
